﻿/*
创建人：zhangrj
日期：2023-08-03
描述：转工排产下发页面js
*/
var page = {
    url: getRootPath() + "/Ashx/ApsHandler/ApsPlanDownSendHandler.ashx", //后台处理url,
    commonUrl: getRootPath() + "/Ashx/CommonHandler.ashx", //通用方法后台路径
    //分页参数
    pagetotal: 0,
    pageSize: 50,
    pageNumber: 1,
    init: function () {
        page.bind();
        page.bindWorkCenter();
        customizedEasyuiPagination('pp'); //定制化分页
    },
    //绑定事件
    bind: function () {
        //列表加载
        $("#gdApsPlanInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'MFGORDERNAME', title: '订单号', width: 120 },
                 { field: 'CONTAINERNAME', title: '批次', width: 150 },
                 { field: 'DESCRIPTION', title: '零件名称', width: 100 },
                 { field: 'QTY', title: '在制数', width: 50 },
                 { field: 'APSQTY', title: '排产数', width: 50 },
                 { field: 'RESOURCENAME', title: '设备编码', width: 100 },
                 { field: 'RESOURCEDESC', title: '设备名称', width: 100 },
                 { field: 'APSPLANEMPNAME', title: '人员', width: 100 },
                 { field: 'WORKFLOWINFO', title: '工艺', width: 170 },
                 { field: 'WORKFLOWNAME', title: '工艺', width: 150, hidden: true },
                 { field: 'SPECNO', title: '工序号', width: 60, hidden: true },
                 { field: 'WORKFLOWSTEPNAME', title: '排产工序', width: 150 },
                 { field: 'CONTAINERTYPENAME', title: '批次类型', width: 80 },
                 { field: 'APSTYPENAME', title: '排产策略', width: 70 },
                 { field: 'STARTTIME', title: '排产开始时间', width: 130,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD HH:mm');
                         }
                     }
                 },
                 { field: 'ENDTIME', title: '排产结束时间', width: 130,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD HH:mm');
                         }
                     }
                 }
                ]]
        });
        //列表分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });
        //查询信息
        $("#btnSearch").on("click", function () {
            page.reset(false, true, true);
            page.pageNumber = 1;
            page.search();
        });
        //重置查询
        $("#btnReset").on("click", function () {
            page.reset(true, true, true);
        });
        //下发
        $("#btnSend").on("click", function () {
            clearMessage();
            page.planSend();
        });
        //回收
        $("#btnReCall").on("click", function () {
            clearMessage();
            page.planReCall();
        });
        //            //删除
        //            $("#btnDele").on("click", function () {
        //                clearMessage();
        //                page.planDele();
        //            });
        //状态下拉事件
        $('#selStatus').combobox({
            onSelect: function (record) {
                if (record.value === "3") {//未下发
                    clearEasyuiDataGrid("gdApsPlanInfo");
                    $("#divSend").show();
                    //                        $("#divDele").show();
                    $("#divReCall").hide();
                } else {//已下发
                    clearEasyuiDataGrid("gdApsPlanInfo");
                    $("#divSend").hide();
                    //                        $("#divDele").hide();
                    $("#divReCall").show();
                }
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            clearMessage();
            var pageNumber = Number($(this).parents('tr').find('.pagination-num').val());
            page.pageNumber = pageNumber;
            page.search();
        });
    },
    //绑定工区
    bindWorkCenter: function () {
        page.post(page.commonUrl, { action: "GetWorkcenterOfDefault" }, true, function (res) {
            //绑定查询工区下拉框
            var workCenter = res.DBTable;
            workCenter.splice(0, 0, { WORKCENTERNAME: "请选择", WORKCENTERID: "", NOTES: null, WCTYPE: null });
            $("#selWorkCenter").combobox({
                valueField: 'WORKCENTERID',
                textField: 'WORKCENTERNAME',
                panelHeight: 200,
                data: workCenter, //res.DBTable, //下拉数据
                onSelect: function (record) {
                    page.bindTeam(record.WORKCENTERID, 1); //加载班组
                }
            });
            if (res.ReturnData != null) {
                $("#selWorkCenter").combobox("setValue", res.ReturnData); //默认值      
            }
            //绑定编辑工区下拉框
            $("#ddlWorkcenter").combobox({
                valueField: 'WORKCENTERID',
                textField: 'WORKCENTERNAME',
                panelHeight: 200,
                data: res.DBTable, //下拉数据
                onSelect: function (record) {
                    page.bindTeam(record.WORKCENTERID, 2); //加载班组
                }
            });
        });
    },
    //绑定查询班组(type:1,查询，2,编辑)
    bindTeam: function (workCenterId, type) {
        page.post(page.commonUrl, { action: "GetTeam", workCenterId: workCenterId }, false, function (res) {
            if (type === 1) {
                //绑定班组
                $("#selTeam").combobox({
                    valueField: 'TEAMID',
                    textField: 'TEAMNAME',
                    panelHeight: 200,
                    data: res.DBTable //下拉数据
                });
                if (res.ReturnData != null) {
                    $("#selTeam").combobox("setValue", res.ReturnData); //默认值      
                }
            }
            else {
                //绑定班组
                $("#ddlTeam").combobox({
                    valueField: 'TEAMID',
                    textField: 'TEAMNAME',
                    panelHeight: 200,
                    data: res.DBTable, //下拉数据
                    onSelect: function (record) { //下拉时动态获取操作工
                        $("#selResource").combobox("clear");
                        page.post(page.url, { action: "GetApsPlanShipOperator", teamID: record.TEAMID }, true, function (res) {
                            $('#selOperator').combogrid({
                                multiple: true, //设置允许多选
                                panelWidth: 150,
                                panelHeight: 200,
                                idField: 'EMPLOYEEID',
                                textField: 'FULLNAME',
                                checkOnSelect: true,
                                columns: [[
                                { field: 'ck', title: '', width: 50, checkbox: true },
                                { field: 'EMPLOYEEID', title: '', width: 60, hidden: true },
                                { field: 'FULLNAME', title: '姓名', width: 110 }
                            ]],
                                data: res.DBTable,
                                onHidePanel: function () {
                                    $("#selResource").combobox("clear");
                                    var rows = $('#selOperator').combogrid('grid').datagrid('getChecked');
                                    var stremployeeid = "";
                                    var resourceData = []; //add:Wangjh 20220302
                                    for (var i = 0; i < rows.length; i++) {
                                        mergerNoRepeat(resourceData, JSON.parse(rows[i].RESOURCES));
                                    }
                                    $("#selResource").combobox({ //绑定设备
                                        valueField: 'RESOURCEID',
                                        textField: 'RESNAME',
                                        panelHeight: 160,
                                        data: resourceData //下拉数据
                                    });
                                }
                            });
                        });
                    }
                });
            }
        });
    },
    //查询
    search: function () {
        clearEasyuiDataGrid("gdApsPlanInfo");
        var params = {
            pageIndex: page.pageNumber,
            pageSize: page.pageSize,
            mfgOrderName: $("#txtOrderNum").textbox("getValue"), //订单号
            containerName: $("#txtContainerNum").textbox("getValue"), //批次号
            productName: $("#txtProductNum").textbox("getValue"), //件号
            specNo: $("#txtSpecNo").textbox("getValue"), //工序号
            startTime1: $("#txtPlanStartTime1").datebox("getValue"), //开始时间
            startTime2: $("#txtPlanStartTime2").datebox("getValue"), //开始时间
            endTime1: $("#txtPlanEndTime1").datebox("getValue"), //结束时间
            endTime2: $("#txtPlanEndTime2").datebox("getValue"), //结束时间
            apsType: $("#selApsType").combobox("getValue"), //排产策略
            workCenter: $("#selWorkCenter").combobox("getValue"), //工区
            team: $("#selTeam").combobox("getValue"), //班组
            status: $("#selStatus").combobox("getValue") //下发状态
        };
        var formData = {
            action: "GetApsPlanShipListByPage",
            params: JSON.stringify(params)
        };
        page.post(page.url, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            page.pagetotal = res.RowCount;
            $("#gdApsPlanInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //下发
    planSend: function () {
        var rows = $("#gdApsPlanInfo").datagrid('getChecked'); //获取勾选的行
        if (rows.length === 0) {
            displayMessage("未选择数据", false);
            return;
        }
        var params = {};
        var datas = $("#editForm").serializeArray();
        $.each(datas, function () {
            params[this.name] = this.value;
        });
        var operator = $("#selOperator").combogrid('grid').datagrid('getChecked');
        var formData = {
            action: "SaveApsPlanShipDatas",
            type: 0,
            datas: JSON.stringify(rows),
            operatorDatas: JSON.stringify(operator),
            params: JSON.stringify(params)
        };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                page.search();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //回收
    planReCall: function () {
        var rows = $("#gdApsPlanInfo").datagrid('getChecked'); //获取勾选的行
        if (rows.length === 0) {
            displayMessage("未选择数据", false);
            return;
        }
        page.post(page.url, { action: "SaveApsPlanShipDatas", type: 1, datas: JSON.stringify(rows) }, true, function (res) {
            if (res.Result) {
                page.search();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult, isEdit) {
        clearMessage();
        if (iscondition) {
            $("#txtOrderNum").textbox("setValue", ""); //订单号
            $("#txtContainerNum").textbox("setValue", ""); //批次号
            $("#txtProductNum").textbox("setValue", ""); //件号
            $("#txtPlanStartTime").textbox("setValue", ""); //排产开始时间
            $("#txtPlanEndTime").textbox("setValue", ""); //排产结束时间
            $("#txtSpecNo").textbox("setValue", ""); //工序号
            $("#selApsType").combobox("setValue", ""); //排产策略
            $("#selStatus").combobox("setValue", "3"); //下发状态
            page.bindWorkCenter();
        }

        if (isresult) {
            clearEasyuiDataGrid("gdApsPlanInfo", 1); //查询结果列表
            page.pageNumber = 1; page.pagetotal = 0; //重置分页
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        }
        if (isEdit) {
            $("#selResource").combobox('clear');
            $("#selOperator").combogrid('clear');
        }
    }
}
$(function () {
    page.init();
})